Repository navigation
[std] Add cond_timed_wait primitive - #336
Merged
Merged
Conversation
Wait on a condition variable for at most the given number of seconds and return false if the timeout expired. Non-positive and NaN timeouts don't block, and very large ones are clamped so that the deadline does not overflow. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Va4FXDYWEo4mt53psuM5hu
Simn
pushed a commit
to HaxeFoundation/haxe
that referenced
this pull request
Nov 1, 2026
hl uses the existing `condition_timed_wait` native and clamps the timeout itself, because older runtimes don't. CI builds the HashLink branch with the native fixes until HaxeFoundation/hashlink#1008 is merged. neko uses the new `cond_timed_wait` primitive when it is available (HaxeFoundation/neko#336), and otherwise polls with growing sleeps, using a counter that `signal` and `broadcast` increment to detect notifications. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Va4FXDYWEo4mt53psuM5hu
13 tasks done
SleepConditionVariableSRW takes a DWORD of milliseconds, so waits of more than about 49.7 days are capped. A capped wait that runs out had not reached the requested timeout, so return true like a spurious wakeup instead of false, which means that the timeout expired. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Va4FXDYWEo4mt53psuM5hu
The Windows timed wait returned true for every error other than ERROR_TIMEOUT, while the POSIX path returns false for any error. Such errors are misuse that should not occur, and returning true would make a predicate loop spin on a persistent error, so return false like on POSIX. Only a capped wait that runs out still counts as a spurious wakeup. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Va4FXDYWEo4mt53psuM5hu
tobil4sk
reviewed
Nov 5, 2026
- gettimeofday follows changes to the system time, which could shorten or lengthen a timed wait. Create condition variables with CLOCK_MONOTONIC and compute the deadline with clock_gettime. macOS has no pthread_condattr_setclock, so use the relative pthread_cond_timedwait_relative_np there. - Give the cond_timed_wait ctest a timeout, so that a hanging wait fails the test instead of the whole run. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Va4FXDYWEo4mt53psuM5hu
tobil4sk
approved these changes
Nov 6, 2026
tobil4sk
left a comment
Member
There was a problem hiding this comment.
Looks good to me now. 60 seconds seems a bit excessive for the timeout (running locally I would probably Ctrl-C after 5-10s), but hopefully this is just a precaution anyway.
Simn
added a commit
to HaxeFoundation/haxe
that referenced
this pull request
Nov 6, 2026
* Add optional timeout to sys.thread.Condition.wait Declare `wait(?timeout:Float):Bool` and document its semantics. The target implementations follow in later commits. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Va4FXDYWEo4mt53psuM5hu * [cpp] Implement Condition.wait with timeout, add tests Use the new hxcpp `ConditionVariable::timedWait`, and install the hxcpp branch that adds it in CI until HaxeFoundation/hxcpp#1456 is merged. Add thread tests for timed waits: timing out with the mutex still held, being signaled, non-positive and NaN timeouts, and huge timeouts. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Va4FXDYWEo4mt53psuM5hu * [hl] [neko] Implement Condition.wait with timeout hl uses the existing `condition_timed_wait` native and clamps the timeout itself, because older runtimes don't. CI builds the HashLink branch with the native fixes until HaxeFoundation/hashlink#1008 is merged. neko uses the new `cond_timed_wait` primitive when it is available (HaxeFoundation/neko#336), and otherwise polls with growing sleeps, using a counter that `signal` and `broadcast` increment to detect notifications. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Va4FXDYWEo4mt53psuM5hu * [jvm] [python] [eval] Implement Condition.wait with timeout The build job's xmldoc step checks every target's std against the core API, so jvm and python must implement the new signature before CI can run any tests. - jvm: Condition.await with nanoseconds, like Semaphore.tryAcquire. - python: threading.Condition.wait, clamped to threading.TIMEOUT_MAX, above which Python raises an OverflowError. - eval: OCaml's Condition has no timed wait, so poll with growing sleeps like StdLock.wait and detect notifications with a counter that signal and broadcast increment. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Va4FXDYWEo4mt53psuM5hu * [ci] Use the hxcpp branch with timedWait for docgen as well The docgen job builds dox for cpp, which includes sys.thread.Condition, and installs hxcpp separately from the cpp tests. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Va4FXDYWEo4mt53psuM5hu * [jvm] Fix Condition.wait with huge timeouts Int64.fromFloat throws for values above 2^53, so a timeout above about 104 days threw while the mutex was held, and testTimedWaitHuge hung. Cap the wait at 9e6 seconds and report a capped wait that runs out as a spurious wakeup instead of a timeout. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Va4FXDYWEo4mt53psuM5hu * [eval] Use libuv mutex and condition for Condition Replace the polling timed wait with Luv.Condition.timedwait, like Lock and Semaphore since #13110, which also removes the notification counter. libuv's mutex is recursive on Windows and aborts the process on misuse on POSIX, while OCaml's mutex raised Sys_error. Track the owning eval thread so that the mutex stays non-recursive on every platform and acquiring twice, or releasing or waiting without holding the mutex, throws a Haxe exception instead. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Va4FXDYWEo4mt53psuM5hu * [ci] Build HashLink from its default branch again HaxeFoundation/hashlink#1008 has been merged. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Va4FXDYWEo4mt53psuM5hu * [ci] Install hxcpp from its default branch again HaxeFoundation/hxcpp#1456 has been merged. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Va4FXDYWEo4mt53psuM5hu * [tests] Allow spurious wakeups in testTimedWaitTimesOut On Windows, MSVC's condition_variable_any::wait_for reports no_timeout when the OS wait returns slightly before the deadline, so a single wait(0.1) can return true. The API allows that as a spurious wakeup, so wait in a loop until a timeout is reported, like callers are expected to. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Va4FXDYWEo4mt53psuM5hu * Split Condition.wait(?timeout) into wait() and timedWait(timeout) As discussed in review: wait() keeps its original signature and documentation, and the timed variant becomes timedWait(timeout):Bool, whose result always means something. This follows Semaphore, which has a blocking acquire():Void and a separate tryAcquire(?timeout):Bool. The target implementations are unchanged apart from the split. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Va4FXDYWEo4mt53psuM5hu * Treat capped timedWait as spurious wakeup on all targets Relax the documentation: very large timeouts may be cut short at a target-specific limit, and such a wait returns true. Make python, cpp, hl and neko return true when the timeout exceeds their cap, as jvm and eval already do. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Va4FXDYWEo4mt53psuM5hu --------- Co-authored-by: Claude <noreply@anthropic.com>
7 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
I've seen the deprecation notice. This is a small, self-contained addition, so I'm opening it in case it can still go in.
Adds
std@cond_timed_wait : 'condition -> timeout:number -> bool. It backs the newsys.thread.Condition.wait(?timeout:Float):Boolin HaxeFoundation/haxe#13109.timeoutseconds and returnsfalseif the timeout expired,trueotherwise (signalled or a spurious wakeup). The mutex is held again on return in both cases.pthread_cond_timedwait, with a 64-bit deadline computed fromgettimeofday, likelock_wait. Windows usesSleepConditionVariableSRWand returnsfalseonERROR_TIMEOUT.INFINITE.Haxe can't depend on this being present, since Neko 2.4.1 and older don't have it. The Haxe side will fall back to a polling loop when the primitive can't be loaded.
Testing
tests/cond_timed_wait.nekoand a ctest entry for it. It checks that a 0.1 s wait times out with the mutex still held, that0,-1, NaN and integer0timeouts returnfalse, and that a1e12s wait returnstruewhen another thread signals it.thread.c, andctest -R cond_timed_waitpasses.Generated by Claude Code